package com.mrd.projects.weixinapi.util;

import com.mrd.projects.weixinapi.entity.ErpAccount;

import org.jsoup.helper.StringUtil;

import java.io.File;
import java.util.Iterator;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * Created by luob on 2015/12/28 0028.
 */
public class WeixinApiConfigUtil {
    private static final String CLASSPATH_PREFIX = "classpath:";
    private static final String CLASSPATH_VALUE;
    private static final ResourceBundle weixinBundle = ResourceBundle.getBundle("weixinapi");
    private static final String WEIXIN4J_PREFIX = "weixinapi";

    static {
        File file = null;
        CLASSPATH_VALUE = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        Iterator i$ = weixinBundle.keySet().iterator();

        while (i$.hasNext()) {
            String key = (String) i$.next();
            if (key.endsWith(".path")) {
                file = new File(getValue(key).replaceFirst("classpath:", CLASSPATH_VALUE));
                if (!file.exists() && !file.mkdirs()) {
                    System.err.append(String.format("%s create fail.%n", new Object[]{file.getAbsolutePath()}));
                }
            }
        }

    }

    public WeixinApiConfigUtil() {
    }

    public static String getServerBaseUrl() {
        return getValue("server.base_url");
    }

    public static String getServerCsrfName() {
        return getValue("server.csrf_name");
    }

    private static String wrapKeyName(String key) {
        return !key.startsWith("weixinapi") ? String.format("%s.%s", new Object[]{"weixinapi", key}) : key;
    }

    public static String getValue(String key) {
        String wrapKey = wrapKeyName(key);
        return System.getProperty(wrapKey, weixinBundle.getString(wrapKey));
    }

    public static String getValue(String key, String defaultValue) {
        String value = defaultValue;

        try {
            value = getValue(key);
        } catch (MissingResourceException var4) {
            System.err.println("\'" + key + "\' key not found in weixinapi.properties file.");
        }

        return value;
    }

    public static String getClassPathValue(String key) {
        return (new File(getValue(key).replaceFirst("classpath:", CLASSPATH_VALUE))).getPath();
    }

    public static String getClassPathValue(String key, String defaultValue) {
        String value = defaultValue;

        try {
            value = getClassPathValue(key);
        } catch (MissingResourceException var4) {
            System.err.println("\'" + key + "\' key not found in weixinapi.properties file.");
        }

        return value;
    }

    public static ErpAccount getAccount() {
        ErpAccount account = null;

        try {
            if (StringUtil.isBlank(getValue("account.appid")) || StringUtil.isBlank(getValue("account.appsecret"))) {
                throw new Exception("appid 或者 appsecret为空.");
            }
            //account = (ErpAccount)JSON.parseObject(getValue("account"), WeixinAccount.class);
            account = new ErpAccount();
            account.setAppid(getValue("account.appid"));
            account.setAppsecret(getValue("account.appsecret"));
        } catch (MissingResourceException var2) {
            System.err.println("\'account\' key not found in weixinapi.properties file.");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return account;
    }
}
